﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using log4net;
using log4net.Config;
using System.Threading;
using log4net.Appender;
using System.Net;
using System.Net.Sockets;

[assembly: log4net.Config.XmlConfigurator(Watch = true)]
namespace TestLogConsoleControl
{
    public partial class Form1 : Form
    {
        private static readonly log4net.ILog log = LogManager.GetLogger(typeof(Form1));
        private System.Windows.Forms.Timer m_timer = null;
        int count = int.MinValue;

        public Form1()
        {
            InitializeComponent();

            log4net.GlobalContext.Properties["tokenlog"] = Guid.NewGuid();

            m_timer = new System.Windows.Forms.Timer();
            m_timer.Interval = 1000;
            m_timer.Tick += new EventHandler(m_timer_Tick);
        }

        void m_timer_Tick(object sender, EventArgs e)
        {
            if ((count % 2) == 0)
            {
                log.Debug(DateTime.Now);
            }
            else if ((count % 3) == 0)
            {
                log.Warn(DateTime.Now);
            }
            else
            {
                log.Error("Errore...................");
            }

            count++;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            m_timer.Start();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            m_timer.Stop();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            IPEndPoint remoteEndPoint = new IPEndPoint(IPAddress.Any, 0);
            UdpClient udpClient;
            byte[] buffer;
            string loggingEvent;

            try
            {
                udpClient = new UdpClient(8080);

                while (true)
                {
                    buffer = udpClient.Receive(ref remoteEndPoint);
                    loggingEvent = System.Text.Encoding.Unicode.GetString(buffer);
                    Console.WriteLine(loggingEvent);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            log4net.ILog log = LogManager.GetLogger("Prova");
            log.Error("prova prova");
        }
    }

}
